package com.cangsan.erhai.security;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cangsan.erhai.common.R;
import com.cangsan.erhai.entity.SysMenu;
import com.cangsan.erhai.entity.SysRole;
import com.cangsan.erhai.entity.SysUser;
import com.cangsan.erhai.service.SysMenuService;
import com.cangsan.erhai.service.SysRoleService;
import com.cangsan.erhai.service.SysUserService;
import com.cangsan.erhai.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * 登录成功处理器
 */
@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
   @Autowired
   private SysUserService userService;
   @Autowired
   private SysRoleService roleService;
   @Autowired
   private SysMenuService menuService;



    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        ServletOutputStream outputStream = httpServletResponse.getOutputStream();
        String user = JwtUtils.genJwtToken(authentication.getName());
        SysUser currnetUser = userService.getByUserName(authentication.getName());


        QueryWrapper<SysRole> getByRoleId = Wrappers.<SysRole>query().inSql("role_id", "select role_id from sys_user_role where user_id=" + currnetUser.getUserId());

        List<SysRole> sysRoles = roleService.list(getByRoleId);


        Set<SysMenu> menuCodeSet= new HashSet<>();

        for (SysRole sysRole : sysRoles) {
            QueryWrapper<SysMenu> get = Wrappers.<SysMenu>query().inSql("menu_id", "select menu_id from sys_role_menu where role_id=" + sysRole.getRoleId());
            List<SysMenu> sysMenus = menuService.list(get);
            for (SysMenu sysMenu : sysMenus) {
                menuCodeSet.add(sysMenu);
            }
        }
        List<SysMenu> sysMenuList = new ArrayList<>(menuCodeSet);

        sysMenuList.sort(Comparator.comparing(SysMenu::getOrderNum));
        List<SysMenu> treeList=  menuService.buildreeMen(sysMenuList);

        R r = R.ok("登录成功");
        Map<String,Object> tokenMap = new HashMap<>();
        tokenMap.put("authorization",user);
        tokenMap.put("menuList",treeList);
        tokenMap.put("currnetUser",currnetUser);
        r.setData(tokenMap);
        String jsonToken = JSONUtil.toJsonStr(r);
        outputStream.write(jsonToken.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
    }
}
